package com.onlyxiahui.pdf.table;

import java.util.ArrayList;
import java.util.List;

import com.onlyxiahui.extend.pdf.table.DataConvert;
import com.onlyxiahui.extend.pdf.table.bean.Row;
import com.onlyxiahui.extend.pdf.table.data.TextPage;
import com.onlyxiahui.extend.pdf.table.data.TextRow;
import com.onlyxiahui.extend.pdf.table.data.TextTable;

/**
 * Description <br>
 * Date 2020-02-04 15:00:27<br>
 * 
 * @author XiaHui [onlovexiahui@qq.com]<br>
 * @since 1.0.0
 */

public class DataConvertImpl1 implements DataConvert {
	int rowStartIndex = 10;
	String space = " ";
	String spaceRegex = " +";
	String tab = "	";

	@Override
	public List<Row> convert(List<TextPage> list) {
		List<Row> rows = new ArrayList<>();
		int pageSize = list.size();
		for (int i = 0; i < pageSize; i++) {
			TextPage p = list.get(i);

			for (TextTable t : p.getTables()) {

				int index = 0;
				for (TextRow r : t.getRows()) {
					index++;
					StringBuilder sb = new StringBuilder();
					for (String c : r.getCells()) {
						sb.append(space);
						sb.append(c);
					}
					String text = sb.toString();
					if (isEndRow(text)) {
						break;
					}
					if (index >= rowStartIndex) {
						Row row = new Row();

						text = text.replace(tab, space);
						text = text.replaceAll(spaceRegex, space);
						text = text.trim();
						String[] array = text.split(space);

						List<String> textList = new ArrayList<>();
						for (String v : array) {
							textList.add(v);
						}
						setRow(row, textList);
						rows.add(row);
					}
				}
			}
		}
		return rows;
	}

	void setRow(Row row, List<String> textList) {
		// int cellSize = 9;
		int markIndex = 4;
		int markIndexTeamp = markIndex;
		int last = 3;
		List<String> list = new ArrayList<>();
		int size = textList.size();

		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < size; i++) {
			String text = textList.get(i);
			if (i == last) {
				sb.append(text);
			}
			if (i < last) {
				list.add(text);
			} else if (i == markIndexTeamp) {
				if (isNumber(text)) {
					list.add(sb.toString());
					list.add(text);
				} else {
					markIndexTeamp++;
					sb.append(space);
					sb.append(text);
				}
			} else if (i > markIndexTeamp) {
				list.add(text);
			}
		}

		for (String v : list) {
			row.addCell(v);
		}
	}

	boolean isEndRow(String text) {
		boolean isEnd = false;
		String t1 = "制单人";
		String t2 = "单数";
		if (null != text) {
			isEnd = text.contains(t1) || text.contains(t2);
		}
		return isEnd;
	}

	public static boolean isNumber(String value) {
		if (null == value || value.isEmpty()) {
			return false;
		}
		char[] chars = value.toCharArray();
		int sz = chars.length;
		boolean hasExp = false;
		boolean hasDecPoint = false;
		boolean allowSigns = false;
		boolean foundDigit = false;
		// deal with any possible sign up front
		int start = (chars[0] == '-') ? 1 : 0;
		if (sz > start + 1) {
			if (chars[start] == '0' && chars[start + 1] == 'x') {
				int i = start + 2;
				if (i == sz) {
					return false; // str == "0x"
				}
				// checking hex (it can't be anything else)
				for (; i < chars.length; i++) {
					if ((chars[i] < '0' || chars[i] > '9')
							&& (chars[i] < 'a' || chars[i] > 'f')
							&& (chars[i] < 'A' || chars[i] > 'F')) {
						return false;
					}
				}
				return true;
			}
		}
		sz--; // don't want to loop to the last char, check it afterwords
		// for type qualifiers
		int i = start;
		// loop to the next to last char or to the last char if we need another
		// digit to
		// make a valid number (e.g. chars[0..5] = "1234E")
		while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
			if (chars[i] >= '0' && chars[i] <= '9') {
				foundDigit = true;
				allowSigns = false;

			} else if (chars[i] == '.') {
				if (hasDecPoint || hasExp) {
					// two decimal points or dec in exponent
					return false;
				}
				hasDecPoint = true;
			} else if (chars[i] == 'e' || chars[i] == 'E') {
				// we've already taken care of hex.
				if (hasExp) {
					// two E's
					return false;
				}
				if (!foundDigit) {
					return false;
				}
				hasExp = true;
				allowSigns = true;
			} else if (chars[i] == '+' || chars[i] == '-') {
				if (!allowSigns) {
					return false;
				}
				allowSigns = false;
				foundDigit = false; // we need a digit after the E
			} else {
				return false;
			}
			i++;
		}
		if (i < chars.length) {
			if (chars[i] >= '0' && chars[i] <= '9') {
				// no type qualifier, OK
				return true;
			}
			if (chars[i] == 'e' || chars[i] == 'E') {
				// can't have an E at the last byte
				return false;
			}
			if (!allowSigns
					&& (chars[i] == 'd'
							|| chars[i] == 'D'
							|| chars[i] == 'f'
							|| chars[i] == 'F')) {
				return foundDigit;
			}
			if (chars[i] == 'l' || chars[i] == 'L') {
				// not allowing L with an exponent
				return foundDigit && !hasExp;
			}
			// last character is illegal
			return false;
		}
		// allowSigns is true iff the val ends in 'E'
		// found digit it to make sure weird stuff like '.' and '1E-' doesn't
		// pass
		return !allowSigns && foundDigit;

	}
}
